<?php

namespace app\home\controller;

use app\common\controller\Home;
use dh2y\qrcode\QRcode;

class Team extends Home
{
    // 定义课程模型
    protected $SubjectModel = null;
    protected $BusinessModel = null;
    // 构造函数
    public function __construct()
    {
        // 手动继承父级的构造函数
        parent::__construct();

        // 加载模型
        $this->SubjectModel = model('subject.Subject');

        $this->BusinessModel = model('business.Business');
    }


    public function poster()
    {

        // 获取旧海报
        $poster = $this->auth->poster ? $this->auth->poster : '';
        $avatar = $this->auth->avatar ? '.' . $this->auth->avatar : '.' . '/assets/img/avatar.png';

        // 生成推广地址 => 通过我们分享地址或者海报进行注册

        // 模仿密码加密方式
        $id = $this->auth->id ?? 0;
        $mobile = $this->auth->mobile ?? '';

        // token => 推荐人的信息
        $token = md5($id . $mobile);

        // 获取当前域名
        $domain = $this->request->domain();
        // halt($domain);

        // 组装推广地址
        $url = $domain . "/home/index/register?token=$token";

        if ($this->request->isPost()) {
            // 实例化生成二维码类
            $QrCode = new QRcode();

            // 生成二维码
            $level = ['L', 'M', 'Q', 'H'];
            //随机生成元素
            $index = array_rand($level);

            // 获取海报的路径
            $path = $QrCode->png($url, false, 5, $index)->logo($avatar)->getPath();

            // 做兼容
            $path = str_replace('\\', '/', $path);

            // 生成海报的路径存储数据表
            $data = [
                'id' => $this->auth->id,
                'poster' => $path
            ];

            // 更新数据表
            $result = $this->BusinessModel->isUpdate(true)->save($data);

            if ($result === false) {
                $this->error('更新海报失败');
            } else {
                is_file('.' . $poster) && @unlink('.' . $poster);

                $this->success('更新海报成功', null, ['poster' => $domain . $path]);
            }
        }

        $this->assign([
            'url' => $url
        ]);

        return $this->fetch();
    }

    public function team()
    {

        // $business = $this->auth;
        if ($this->request->isAjax()) {
            $page = $this->request->param('page',1,'trim');
            $limit = $this->request->param('limit',10,'trim');
            $search = $this->request->param('search','','trim');

            $where = [
                'parentid' => $this->auth->id
            ];

            // 如果搜索关键字不为空
            if (!empty($search)) {
                // 组装条件
                $where['nickname'] = ['LIKE',"%$search%"];
            }

            $start = ($page - 1) * $limit;

            $list = $this->BusinessModel->where($where)->page($start,$limit)->select();

            //查询数据总数
            $count = $this->BusinessModel->where($where)->count();

            $data=[
                'list' => $list,
                'count' => $count
            ];

            if($list)
            {
                $this->success('查询数据成功',null,$data);
            }else{
                $this->error('暂无数据',null,$data);
            }
        }

        // $subjectList = $this->SubjectModel->order('id desc')->limit(10)->select();


        // $this->assign([
        //     'subjectList' => $subjectList
        // ]);

        return $this->fetch();
    }
}
